<template>
  <view>
    <view class="goods-list">
      <view @click="gotoGoodsDetail(goods)" v-for="(goods, i) in goodsList" :key="i">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //节流阀
        isloading: false,
        goodsList: [],
        total: 0,
        //请求参数对象
        queryObj: {
          //关键字
          query: '',
          //id
          cid: '', 
          //默认请求第一页数据
          pagenum: 1,
          //每页长度
          pagesize: 10
        }
        
      }
    },
    //options跳转到该页面所携带的参数
    onLoad (options) {
      this.getGoodsList()
      // console.log(options);
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
    },
    methods: {
      gotoGoodsDetail (goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      },
      //触发下拉刷新事件
      enablePullDownRefresh () {
        //重置数据
        this.queryObj.pagenum = 1
        this.total = 0
        this.goodsList = []
        this.isloading = false
        //重新发请求，获取数据,成功后，关闭下拉框uni.stopPullDownRefresh()
        this.getGoodsList(() => uni.stopPullDownRefresh())
      },
      //触发上拉触底事件
      onReachBottom () {
        //数据加载完毕，显示提示信息
        if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('没有更多数据了')
        //判断节流阀是否关闭，关闭节流阀，才能重新发请求获取数据
        if(this.isloading) return
        //页面加1
        this.queryObj.pagenum += 1
        this.getGoodsList()
      },
      //获取商品列表数据
      async getGoodsList (cd) {
        //开启节流阀
        this.isloading = true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        //数据获取成功后，关闭下拉框
        cd && cd()
        //关闭节流阀
        this.isloading = false
        if(res.meta.status !== 200) return uni.$showMsg()
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      }
      
    }
  }
</script>

<style lang="scss">
</style>
